package com.macro.mall.portal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.portal.domain.AddPmsProductComment;
import com.macro.mall.portal.domain.PmsProductCommentRes;
import com.macro.mall.portal.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private PmsCommentMapper pmsCommentMapper;


    @Override
    public void addProductCommentData(AddPmsProductComment addPmsProductComment) {
        PmsComment pmsComment = new PmsComment();
        pmsComment.setProductId(addPmsProductComment.getProductId());
        pmsComment.setMemberNickName(addPmsProductComment.getMemberNickName());
        pmsComment.setMemberIcon(addPmsProductComment.getMemberIcon());
        pmsComment.setCreateTime(new Date());
        pmsComment.setContent(addPmsProductComment.getContent());
        pmsComment.setStar(addPmsProductComment.getStarLevel());
        pmsComment.setShowStatus(1);
        pmsComment.setProductAttribute(addPmsProductComment.getProductData());
        pmsComment.setCollectCouont(0);
        pmsComment.setReadCount(0);
        if (addPmsProductComment.getAnonymousFlag() == 1){
            pmsComment.setMemberNickName("匿名用户");
            pmsComment.setMemberIcon("https://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/2018/12/25/e0a1c8b9-f9c4-4d09-b9b7-d7c4c7d0e0f5.png");
        }
        if (CollUtil.isNotEmpty(addPmsProductComment.getPicList())){
            pmsComment.setPics(CollUtil.join(addPmsProductComment.getPicList(), ","));
        }
        pmsComment.setReplayCount(0);
        pmsCommentMapper.insert(pmsComment);
    }

    @Override
    public PmsProductCommentRes selProductCommentData(Long productId) {
        //获取商品评价列表
        PmsProductCommentRes pmsProductCommentRes = new PmsProductCommentRes();
        //查询全部评价列表
        PmsCommentExample pmsCommentExample = new PmsCommentExample();
        pmsCommentExample.createCriteria().andProductIdEqualTo(productId);
        //商品的所有评价
        List<PmsComment> pmsComments = pmsCommentMapper.selectByExampleWithBLOBs(pmsCommentExample);
        //全部
        pmsProductCommentRes.setAllProductComments(pmsComments);
        pmsProductCommentRes.setAllSum(pmsComments.size());
        //好评
        pmsProductCommentRes.setGoodProductComments(pmsComments.stream().filter(pmsComment -> pmsComment.getStar() >= 4).collect(Collectors.toList()));
        pmsProductCommentRes.setGoodSum(pmsProductCommentRes.getGoodProductComments().size());
        //中评
        pmsProductCommentRes.setMediumProductComments(pmsComments.stream().filter(pmsComment -> pmsComment.getStar() == 3).collect(Collectors.toList()));
        pmsProductCommentRes.setMediumSum(pmsProductCommentRes.getMediumProductComments().size());
        //差评
        pmsProductCommentRes.setBadProductComments(pmsComments.stream().filter(pmsComment ->  pmsComment.getStar() < 3).collect(Collectors.toList()));
        pmsProductCommentRes.setBadSum(pmsProductCommentRes.getBadProductComments().size());
        //晒图
        pmsProductCommentRes.setPicProductComments(pmsComments.stream().filter(pmsComment -> pmsComment.getPics() != null && !pmsComment.getPics().isEmpty()).collect(Collectors.toList()));
        pmsProductCommentRes.setPicSum(pmsProductCommentRes.getPicProductComments().size());
        return pmsProductCommentRes;
    }
}
